
package com.hockeo.tinygds;

import java.lang.reflect.Field;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hockeo.tinygds.error.TNotPersistableException;

/**
 * Represents a String primary key field.
 * 
 * @version $Id$
 * @author jjanke
 */
public class TFieldPkString extends TFieldPrimaryKey<String>
{

  protected TFieldPkString( TinyGDSFactory factory, Field field )
  {
    super( factory, field );
  }

  public Entity createEntity( Object objInstance, String strKind, Key keyParent )
  {
    String strId = getValue( objInstance );
    
    if ( strId == null )
      throw TNotPersistableException.objectHasNullStringId( getRealField(), objInstance );
    
    return new Entity( strKind, strId, keyParent );
  }
  
  public Key getKey( Object objInstance, String strKind, Key keyParent )
  {
    return KeyFactory.createKey( keyParent, strKind, getValue( objInstance ) );
  }
  
  public void setKey( Object objInstance, Key key )
  {
    setValue( objInstance, key.getName() );
  }
}
